package com.flower.algorithm.queue;

/**
 * @ClassName CircularQueue
 * @Description
 * @Author 吃麻椒的花酱
 * @Date 2021/10/27 11:20
 * @Version 1.0
 */
public class CircularQueue {
    int maxSize;
    int rear;// 指向当前队头元素
    int front;// 指向当前队尾的下一个元素
    int[] array;
    // 初始化队列
    public CircularQueue(int maxSize){
        this.rear=0;
        this.front = 0;
        this.maxSize = maxSize;
        this.array = new int[maxSize];
    }
    // 判断是否队空
    public boolean isEmpty(){
        return this.rear==this.front;
    }
    // 判断是否队满
    public boolean isFull() {
        return (this.rear+1)%maxSize==this.front;
    }
    // 入队列
    public void addQueue(int num){
        if(isFull()){
            throw new RuntimeException("队列已满");
        }
        this.array[this.rear]=num; // 注意理解
        this.rear=(this.rear+1)%maxSize;
    }
    public int poll() {
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int temp= this.array[this.front];
        this.front=(this.front + 1)%maxSize;
        return temp;
    }
    public int size(){
        return (this.rear-this.front+maxSize)%maxSize;
    }
    public void showQueue() {
        for (int i = front; i < this.front+this.size();i++) {
            System.out.println(array[i%maxSize]);
        }
    }
}
class Queuess {
    int maxSize;
    int front;// 指向当前队尾元素
    int count; // 当前队列元素的个数
    int[] array;
    // 初始化队列
    public Queuess(int maxSize){
        //  this.rear=0;
        this.front = 0;
        this.maxSize = maxSize;
        this.count = 0;
        this.array = new int[maxSize];
    }
    // 判断是否队空
    public boolean isEmpty(){
        return this.count==0;
    }
    // 判断是否队满
    public boolean isFull() {
        return this.count==this.maxSize;
    }
    // 入队列
    public void addQueue(int num){
        if(isFull()){
            throw new RuntimeException("队列已满");
        }
        this.array[(this.front+this.count)%maxSize]=num; // 注意理解
        this.count++;
    }
    public int poll() {
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int temp= this.array[this.front];
        this.front=(this.front + 1)%maxSize;
        this.count--;
        return temp;
    }
    public int size(){
        return this.count;
    }
    public void showQueue() {
        for (int i = front; i < this.front+this.size();i++) {
            System.out.println(array[i%maxSize]);
        }
    }
}